package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.events.EventRunBack;
import com.myk.game.heroscuffle.events.HeroEvent_AfterBeAttack;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;
import com.myk.game.heroscuffle.signs.CountSign_DongShang;
import com.myk.game.heroscuffle.utils.RdmPropUtil;

public class Skill_XueYin extends Skill {

    private static final int TEMP_ADDITION_DEF = 30; //临时增加自己的的防御力数值
    private boolean isHidden = false; //是否处于雪隐状态
    private EventRunBack<HeroEvent_AfterBeAttack> afterBeAttackEventRunBack; //雪隐状态监听被攻击的事件回调

    public Skill_XueYin() {
        super("雪隐",
                "切换为雪隐状态临时增加大量防御力。此时攻击自己的敌人都会被添加冻伤标记，减少一定的攻击力和防御力。敌人可能会因为惧怕放弃对自己的追捕。",
                1, Integer.MAX_VALUE);
    }

    @Override
    protected void onTrigger() {
        isHidden = true;
        hostHero.def += TEMP_ADDITION_DEF;
        game.ui.printMsg("用冰雪保护自己，临时增加" + TEMP_ADDITION_DEF + "点def,当前" + hostHero.def + "点。");
        afterBeAttackEventRunBack = eventData -> {
            game.ui.sleepShortly();
            game.ui.printMsg(eventData.attacker.getObviousName() + "被冰雪冻伤寒气入体，");
            eventData.attacker.pasteSign(new CountSign_DongShang()); //添加冻伤标记减少攻防
            if (RdmPropUtil.bingo(3 / 4d)) {
                Hero master = eventData.attacker; //攻击者
                Hero slave = hostHero; //技能的宿主
                eventData.attacker.event.onAfterDoAttack.addRunBack(eventData1 -> { //由于攻击者是在攻击行为完成后才记录仇恨的，只能用这种方式注册监听，才能在最后清除仇恨
                    game.ui.printMsg(master.getObviousName() + "发现" + slave.getObviousName() + "不好惹，放弃了追捕。");
                    game.ui.sleepShortly();
                    master.clearPrey();
                    eventData1.removeRunBackFromListener();
                });
            }
        };
        hostHero.event.onAfterBeAttack.addRunBack(afterBeAttackEventRunBack);
    }

    @Override
    protected void onUpdate() {
        if (isHidden) {
            isHidden = false;
            hostHero.def -= TEMP_ADDITION_DEF;
            game.ui.printlnMsg("冰雪消融" + hostHero.getObviousName() + "def恢复至" + hostHero.def + "点。");
            hostHero.event.onAfterBeAttack.removeRunBack(afterBeAttackEventRunBack);
        }
    }

    @Override
    protected void onInit() {
        remnantCoolingTime = 0;
    }
}
